//
//  GYModifyPwdView.m
//  GeYou
//
//  Created by keven kenv on 2017/11/30.
//  Copyright © 2017年 geyou. All rights reserved.
//

#import "GYModifyPwdV.h"
#import "Head.h"

@interface GYModifyPwdV ()
@property (nonatomic, assign) BOOL isShowPwd;
@property (nonatomic, assign) BOOL isShowPwd_1;
@property (weak, nonatomic) IBOutlet UIButton *showOldPwdBtn;
@property (weak, nonatomic) IBOutlet UIButton *showNewPwdBtn;
@property (weak, nonatomic) IBOutlet UIButton *saveBtn;

@end

@implementation GYModifyPwdV

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.saveBtn.backgroundColor = RGBACOLOR(221, 221, 221, 1);
    self.saveBtn.layer.cornerRadius = 4;
    self.saveBtn.layer.masksToBounds = YES;
    
    self.isShowPwd = NO;
    self.isShowPwd_1 = NO;
    self.oldPwdF.secureTextEntry = YES;
    self.nPwdF.secureTextEntry = YES;
    
    if ([self checkPwd]) {
        [self.saveBtn canClick];
        [self.saveBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        self.saveBtn.backgroundColor = RGBACOLOR(184, 26, 8, 1);
    } else {
        [self.saveBtn cannotClick];
        [self.saveBtn setTitleColor:RGBACOLOR(155, 155, 155, 1) forState:UIControlStateNormal];
        self.saveBtn.backgroundColor = RGBACOLOR(221, 221, 221, 1);
    }
    
    [self.oldPwdF addTarget:self action:@selector(textFieldDidChanged:) forControlEvents:UIControlEventEditingChanged];
    [self.nPwdF addTarget:self action:@selector(textFieldDidChanged:) forControlEvents:UIControlEventEditingChanged];
}

#pragma mark - event responds
- (void)textFieldDidChanged:(UITextField *)textField {
    if ([self checkPwd]) {
        [self.saveBtn canClick];
        [self.saveBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        self.saveBtn.backgroundColor = RGBACOLOR(184, 26, 8, 1);
    } else {
        [self.saveBtn cannotClick];
        [self.saveBtn setTitleColor:RGBACOLOR(155, 155, 155, 1) forState:UIControlStateNormal];
        self.saveBtn.backgroundColor = RGBACOLOR(221, 221, 221, 1);
    }
}


- (IBAction)showOldPwdAction:(id)sender {
    if (self.isShowPwd) {
        self.oldPwdF.secureTextEntry = NO;
        [self.showOldPwdBtn setImage:[UIImage imageNamed:@"browse_fill (1)"] forState:UIControlStateNormal];
    } else {
        self.oldPwdF.secureTextEntry = YES;
        [self.showOldPwdBtn setImage:[UIImage imageNamed:@"browse_fill "] forState:UIControlStateNormal];
    }
    self.isShowPwd = !self.isShowPwd;
}

- (IBAction)showNewPwdAction:(id)sender {
    if (self.isShowPwd_1) {
        self.nPwdF.secureTextEntry = NO;
        [self.showNewPwdBtn setImage:[UIImage imageNamed:@"browse_fill (1)"] forState:UIControlStateNormal];
    } else {
        self.nPwdF.secureTextEntry = YES;
        [self.showNewPwdBtn setImage:[UIImage imageNamed:@"browse_fill "] forState:UIControlStateNormal];
    }
    self.isShowPwd_1 = !self.isShowPwd_1;
}

- (IBAction)saveAction:(id)sender {
    if (self.delegate && [self.delegate respondsToSelector:@selector(saveNewPwd:)]) {
        [self.delegate saveNewPwd:self];
    }
}

#pragma mark - private methods
- (BOOL)checkPwd {
    NSString *pwd = [self.oldPwdF.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSString *pwd_1 = [self.nPwdF.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    if (pwd.length >= 6 && pwd_1.length >= 6) {
        return YES;
    }
    
    return NO;
}

//- (BOOL)checkStrWithString:(NSString *)str {
//    NSString * regex = @"^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$";
//    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
//    BOOL result = [pred evaluateWithObject:str];
//    return result;
//}

@end
